package com.kevis.zhunblog.core.entities;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;


/**
 * 类别基础父类
 *
 * @Author kevis
 * @Date 2023/4/19 16:02
 */
@Data
public class BaseClassEntity implements Serializable {
    /**
     * 主键自增
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    /**
     * 创建时间
     */
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @TableField(value = "create_time")
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @TableField(value = "update_time")
    private LocalDateTime updateTime;

    /**
     * 状态
     */
    @TableField(value = "status")
    private Integer status;

    /**
     * 分类名称
     */
    @TableField(value = "title")
    private String title;

    /**
     * 分类等级
     */
    @TableField(value = "class_level")
    private Integer classLevel;

    /**
     * 父级id
     */
    @TableField(value = "parent_id")
    private Integer parentId;

    /**
     * 分类路径
     */
    @TableField(value = "class_path")
    private String classPath;

    /**
     * 排序,默认6666
     */
    @TableField(value = "sort")
    private Integer sort;

    /**
     * 是否存在子节点
     */
    @TableField(value = "exist_children")
    private Boolean existChildren;

    /***********以下为数据库不存在字段**********/

    /*
     * children
     */
    @TableField(exist = false)
    public List<BaseClassEntity> children;

    @TableField(exist = false)
    public int childcount;

    public int getChildcount() {
        return (this.getChildren() == null) ? 0 : this.getChildren().size();
    }

    public void setChildcount(int childcount) {
        this.childcount = childcount;
    }

    /*
     * Tree状态
     */
    @TableField(exist = false)
    public String state;

    public String getState() {
        return (this.getChildcount() == 0) ? "open" : "closed";
    }

    public void setState(String state) {
        this.state = state;
    }

    /*
     * 上级栏目
     */
    @TableField(exist = false)
    public String e_parentTitle;
}
